package com.lagou;

import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class Test2_api {

    private void testString(){
        Jedis jedis = new Jedis("192.168.106.128",6379);
        jedis.set("k1","v1");
        jedis.set("k2","v2");
        jedis.set("k3","v3");
        Set<String> keys = jedis.keys("*");
        Iterator<String> iterator = keys.iterator();

        for(keys.iterator();iterator.hasNext();){
            String k = iterator.next();
            System.out.println(k+"->"+jedis.get(k));
        }

        Boolean k2 = jedis.exists("k2");
        System.out.println("是否存在"+k2);
        // 查看k1的过期时间
        System.out.println(jedis.ttl("k1"));
    }

    private void testSet(){
        Jedis jedis = new Jedis("192.168.106.128",6379);
        jedis.mset("k4","v4","k5","v5");
        System.out.println(jedis.mget("k1","k2","k3","k4","k5"));
        System.out.println("---------------------------------------------");
        // list
        jedis.lpush("list01","l1","l2","l3","l4","l5");
        List<String> list = jedis.lrange("list01",0,-1);
        for (String s : list) {
            System.out.println(s);
        }
        System.out.println("------------------------------------");
        // set
        jedis.sadd("oreder","jd001");
        jedis.sadd("oreder","jd002");
        jedis.sadd("oreder","jd003");
    }

    private void testHash(){
        Jedis jedis = new Jedis("192.168.106.128",6379);
        jedis.hset("user1","username","james");
        System.out.println(jedis.hget("user1","username"));
        HashMap<String,String> map = new HashMap<String, String>();
        map.put("username","tom");
        map.put("age","18");
        jedis.hset("user2",map);
        List<String> user2 = jedis.hmget("user2","username","age");
        for (String s : user2) {
            System.out.println(s);
        }
    }

    private void testZset(){
        Jedis jedis = new Jedis("192.168.106.128",6379);
        jedis.zadd("zset01",60d,"zs1");
        jedis.zadd("zset01",70d,"zs2");
        jedis.zadd("zset01",80d,"zs3");
        jedis.zadd("zset01",90d,"zs4");
        Set<String> zset01 = jedis.zrange("zset01", 0, -1);
        Iterator<String> iterator = zset01.iterator();
        while(iterator.hasNext()){
            String next = iterator.next();
            System.out.println(next);
        }
    }


    public static void main(String[] args) {
        new Test2_api().testZset();

    }
}
